/**
 * 快速表单编辑插件
 *
 集成到dwz在dwz.ui.js中加入以下代码代码
 if ($.fn.feText) $("span[target=feText]", $p).feText();
 if ($.fn.feSelect) $("span[target=feSelect]", $p).feSelect();
 if ($.fn.feToggleState) $("span[target=feToggleState]", $p).feToggleState();
 使用方法举例
 <span feId="{$vo['id']}" target="feText" name="sort" href="__URL__/setField/field/sort/">{$vo['sort']|default="无"}</span>
 <span feId="{$vo['id']}" target="feToggleState" name="sort" href="__URL__/setField/field/state/">{$vo['state']|default="0"}</span>
 *
 * @category   javascript
 * @package  fastEdit
 * @Author: yuanji <fenglangyj@139.com>
 * @DateTime: 2012年11月12日17:13:26
 * @Copyright (c) 2012 http://www.magentoui.com All rights reserved.
 * @Version  $Id: h.php,v 1.0 uw Exp $
 * @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 */


(function($){
    $.extend($.fn, {
        /**
         * 文本编辑框
         * @param options
         */
        feText:function(options) {
            $(this).click(function(){
                $this = $(this);
                //如果已经在编辑状态，不做任何操作
                var tag = this.firstChild.tagName;
                if (typeof(tag) != "undefined" && tag.toLowerCase() == "input"){
                    return;
                }
                /* 保存原始的内容 */
                var org = $this.html();
                var val = org;
                /* 创建一个输入框 */
                var txt = document.createElement("INPUT");
                txt.value = (val == 'N/A') ? '' : val;
                txt.style.width = (this.offsetWidth + 12) + "px" ;
                /* 隐藏对象中的内容，并将输入框加入到对象中 */
                this.innerHTML = "";
                this.appendChild(txt);
                txt.select();
                $(txt).keypress(function(evt){
                    if (evt.keyCode == 13){
                        this.blur();
                        return false;
                    }
                    if (evt.keyCode == 27){
                        this.parentNode.innerHTML = org;
                    }
                });

                /* 编辑区失去焦点的处理函数 */
                txt.onblur = function(e){
                    if ($.trim(txt.value).length > 0 && $.trim(txt.value) != org){
                        $id= $this.attr('id');
                        $data = new Object();
                        if ($this.attr('name')) $data.name = $this.attr('name');
                        if ($.trim(txt.value)) $data.value= $.trim(txt.value);
                        if ($this.attr('feId')) $data.id= $this.attr('feId');
                        $.getJSON($this.attr('href'),$data,function(res){
                            if(res.message){
                                alertMsg.info(res.message);
                            }
                            if(DWZ.statusCode.error == res.statusCode){
                                $this.html(( res.statusCode == DWZ.statusCode.ok) ? $data.value:org);
                            }else{
                                $this.html(( res.statusCode == 200) ? $data.value:org);
                            }
                        });
                    }else{
                        $this.html(org);
                    }
                }
            });
        },
        /**
         * 状态切换按钮
         */
        feToggleState:function() {
            return this.each(function(){
                var $this = $(this);
                var $val = parseInt($.trim($this.html()));
                if($val==1){
                    $this.html('YES');
                    $this.css("color","green")
                }else{
                    $this.html('NO');
                    $this.css("color","red")
                }
                $this.css("cursor","pointer");
                $this.click(function(){
                    var $id= $this.attr('id');
                    var $data = new Object();
                    $data.value= $val==0?1:0;
                    if ($this.attr('name')) $data.name = $this.attr('name');
                    if ($this.attr('feId')) $data.id= $this.attr('feId');
                    $.getJSON($this.attr('href'),$data,function(res){
                        if(res.message){
                            //if(jQuery.isFunction('alertMsg')){
                            if(alertMsg){
                                alertMsg.info(res.message);
                            }else{
                                alert(res.message);
                            }
                        }
                        if(DWZ.statusCode.ok == res.statusCode){
                            $val = $data.value;
                            if($data.value == 1){
                                $this.html('YES');
                                $this.css("color","green")
                            }else{
                                $this.html('NO');
                                $this.css("color","red")
                            }
                        }else{
                            $val==0?1:0;
                        }
                    });
                });
            });
        },
        feSelect:function(){
            return this.each(function(){
                var $this = jQuery(this);
                /* 保存原始的内容 */
                var org = $this.html();
                var val = org;
                var $data_source = jQuery(jQuery($this.attr('data_source')).get(0)).clone();
                $this.html($data_source.children("option[value="+val+"]").text());
                jQuery(this).click(function(){
                    //如果已经在编辑状态，不做任何操作
                    var tag = this.firstChild.tagName;
                    if (tag != "undefined" && tag == "SELECT"){
                        return false;
                    }
                    /* 加入数据源的数据 */
                    $this.empty();
                    $data_source.appendTo($this);
                    $data_source.val(val);
                    $data_source.change(function(){
                        var $id= $this.attr('id');
                        var $data = new Object();
                        if ($this.attr('name')) $data.name = $this.attr('name');
                        $data.value= jQuery(this).val();
                        if ($this.attr('feId')) $data.id= $this.attr('feId');
                        jQuery.getJSON($this.attr('href'),$data,function(res){
                            if(res.message){
                                if(typeof(alertMsg)!='undefined'){
                                    alertMsg.info(res.message);
                                }else{
                                    alert(res.message);
                                }
                            }
                            if(typeof(DWZ)!='undefined'){
                                if(res.statusCode==DWZ.statusCode.ok){
                                    $this.html($data_source.children("option[value="+res.data+"]").text());
                                }else{
                                    $this.html($data_source.children("option[value="+org+"]").text());
                                }
                            }else{
                                if(res.statusCode==200){
                                    $this.html($data_source.children("option[value="+res.data+"]").text());
                                }else{
                                    $this.html($data_source.children("option[value="+org+"]").text());
                                }
                            }
                        });
                    });
                });
            });
        }
    });
})(jQuery);
